package com.tjy.farmer.domain;

import java.util.Objects;

/**
 * 经纬度位置模型
 */
public class Location {
    private double longitude; // 经度
    private double latitude;  // 纬度
    private Integer pointOrder; // 顺序

    public Location(double longitude, double latitude, Integer pointOrder) {
        this.longitude = longitude;
        this.latitude = latitude;
        this.pointOrder = pointOrder;
    }

    // getter和setter方法
    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public Integer getPointOrder() {
        return pointOrder;
    }

    public void setPointOrder(Integer pointOrder) {
        this.pointOrder = pointOrder;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Location location = (Location) o;
        // 注意：浮点数比较不能用==，需用差值小于极小值（如1e-6）避免精度问题
        return Math.abs(location.longitude - longitude) < 1e-6
                && Math.abs(location.latitude - latitude) < 1e-6;
    }

    // 👇 重写hashCode：与equals逻辑一致，基于经度和纬度生成
    @Override
    public int hashCode() {
        return Objects.hash(
                Math.round(longitude * 1e6), // 放大后取整，避免浮点数精度影响
                Math.round(latitude * 1e6)
        );
    }
}
